<?php

class Authentication extends Controller{
	
	public $autoRender = false;
	
	private static $_instance;
	private static function _getInstance() {
		if(!self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public static function getUser(){
		return $_SESSION["user"];
	}
	
	public function login() {
		$user = $this->Model(User)->findFirst(array(
			"filter" => array(
				"username" => $this->data["username"],
				"password" => $this->hash($this->data["password"])
			)
		));
		if($user) {
			$_SESSION["user"] = $user;
		}else{
			$this->setFlash("Login failed.", "error");
		}
		redirect();
	}
	
	public function logout(){
		unset($_SESSION["user"]);
		redirect();
	}
	
	private function hash($string) {
		return md5($string);
	}
}

?>